(function(){
	var pagenum=10;//每页数量
	var curpage=1;//当前页面
	var totalPage=0;//总页数

	var params = {
		teamName : "" ,  				//队伍名称
		leagueName : "",				//联赛名称
		marketName : "" ,
		beginDate : "" ,
		endDate : "" ,
		offset : 0 ,
		limit : pagenum
	}
	var doneId = "";
	var lottList = [];
	
	//获取列表
	function getlist(curpage,pagenum){
		$(".box-body").mLoading();
	   $.ajax({
			url: httpOrigin + "/third/lottery/list.json",
			type:'POST',
			async:true ,
			data:params ,
			dataType: "json",
			success: function(data){
				var html = "";
				if(data.code === 200 ){
					lottList = data.t.result;

					if(lottList && lottList.length > 0){
						lottList.forEach(function ( item ,index ) {
							var tr = '',first = false,inner = "",inner2 = "",tr2 = "";
							var time = new Date(item.startTime).format("yyyy-MM-dd hh:mm:ss");



							item.tapupMarketInfos.forEach(function ( val ,key ) {

								var goldType = "",status = "",isShow = "";
								if( val.goldType == 1 ) {
									goldType = "金币";
								} else if ( val.goldType == 0 ) {
									goldType = "钻石";
								}
								if( val.status == 0 ) {
									status = "竞猜中";
								} else if( val.status == 3 ) {
									status = "暂停中";
								} else if(  val.status == 4 ) {
									status = "已结束";
								}
								if( val.isShow == 1 ) {
									isShow = "是";
								}
								if( val.isShow == 0 ) {
									isShow = "否";
								}
								if( !first ) {
									inner += "<td>" + val.name + "</td>" +
											"<td>" + ( Boolean( val.teamValue ) ? val.teamValue : "" ) + "</td>" +
											"<td>" + status + "</td>" +
											"<td>" + val.redCount + "</td>" +
											"<td>" + val.blueCount + "</td>";
									first = true;
								} else {
									tr += "<tr role=\"row\">" +
												"<td>" + val.name + "</td>" +
												"<td>" + ( Boolean( val.teamValue ) ? val.teamValue : "" ) + "</td>" +
												"<td>" + status + "</td>" +
												"<td>" + val.redCount + "</td>" +
												"<td>" + val.blueCount + "</td>" +
												"<td class=\"toggle-show\" data-id=\""+ val.marketId +"\" data-show=\""+ val.isShow +"\">"+ isShow +"</td>" +
											"</tr>";
								}
							}, this );
							html += "<tr role=\"row\">" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">三方</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">"+ item.matchId +"</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">"+ item.tournamentName + "</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">"+ item.homeTeamName +" vs " + item.awayTeamName + "</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">"+ item.quizId + "</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\">"+ time +"</td>" +
										inner +
										"<td class=\"toggle-show\" data-id=\""+ item.tapupMarketInfos[0].marketId +"\" data-show=\""+ item.tapupMarketInfos[0].isShow +"\">"+ ( item.tapupMarketInfos[0].isShow == 1 ? '是' : '否' ) + "</td>" +
										"<td rowspan=\""+ item.tapupMarketInfos.length +"\" class=\"sort-top\" data-id=\""+ item.matchId +"\">"+ item.sortId +"</td>" +
									"</tr>" +
									tr;
						} , this )


					   totalPage=data.t.totalPage;
					   $(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
					   $(".curpage").val(curpage);
					   if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
					   if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					} else {
						html += "<tr><td colspan='14'>暂无数据</td></tr>";

						totalPage=data.t.totalPage;
						$(".pageinfo").html("当前"+curpage+"页/共"+totalPage+"页");
						$(".curpage").val(curpage);
						if(curpage==1){$(".prev").addClass("disabled")}else{$(".prev").removeClass("disabled")}
						if(curpage >= totalPage){$(".next").addClass("disabled")}else{$(".next").removeClass("disabled")}
					}
					$("#example1 tbody").html( html );
				}

			} ,
		   complete : function () {
			   $(".box-body").mLoading("hide");
		   }
	   });
	}
	//显示
	function mostShow( _params ) {
		$(".box-body").mLoading();
		$.ajax({
			url : httpOrigin + "/third/lottery/isShow.json" ,
			type : "POST" ,
			data : _params ,
			dataType : "json" ,
			success : function ( data ) {
				if( data.code == 200 ) {
					getlist(curpage, totalPage);
				} else {
					$(".box-body").mLoading("hide");
					alert( data.msg )
				}
			}
		})
	}
	//排序接口
	function setLottSort( _params ){
		$(".box-body").mLoading();
		$.ajax({
			url : httpOrigin + "/third/lottery/update/sortId.json" ,
			type :"POST" ,
			data : _params ,
			success : function ( data ) {
				if( data.code == 200 ) {
					getlist(curpage, totalPage);
				} else {
					$(".box-body").mLoading("hide");
					alert( data.msg )
				}

			}
		})
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//上一页
		    $(".prev").click(
			    function(){
					if(curpage>1){
				     	curpage=curpage-1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//下一页
		    $(".next").click(
			    function(){
					if(curpage<totalPage){
				     	curpage=curpage+1;
						params.offset = ( curpage - 1 ) * params.limit;
					    getlist(curpage,pagenum);
					}
			    }
			)
			//页码跳转
			$(".gopage").click(
			    function(){
					if($(".curpage").val()>=1&&$(".curpage").val()<=totalPage){
						curpage=parseInt($(".curpage").val());
						params.offset = ( curpage - 1 ) * params.limit;
						getlist(curpage,pagenum)
					}else{
						$(".curpage").val(curpage);
					}
			    }
			)
			//显示隐藏
			$("#example1").on( "click" ,".toggle-show",function () {
				var id =  $( this).get(0).dataset.id,
					show = $(this).get(0).dataset.show,
					toggle = 0;
				var tip = "";
				if( show == 1 ) {
					tip = "确定隐藏？";
					toggle = 0;
				} else {
					tip = "确定显示？";
					toggle = 1;
				}
				var result = window.confirm( tip );

				if( result ) {
					mostShow({ marketIds : id , isShow : toggle });
				}
			})
			//排序
			$("#example1").on( "click" ,".sort-top",function () {
				var id =  $( this).get(0).dataset.id;
				var result = window.prompt( "请输入权重" );

				if( result ) {
					setLottSort({ matchId : id , sortId : result });
				}
			})
			//比赛队伍
			$(".player").change(function () {
				params.teamName = $(this).val();
			})
			//联赛名称
			$(".league").change(function () {
				params.leagueName = $(this).val();
			})
			//竞猜场次
			$(".market").change(function () {
				params.marketName = $(this).val();
			})
			//开始时间
			$("#datepicker").change(function () {
				params.beginDate = $(this).val();
			})
			//结束时间
			$("#datepicker2").change(function () {
				params.endDate = $(this).val();
			})
			//搜索按钮
			$(".search").click(function () {
				params.offset = 0;
				curpage = 1;
				getlist(curpage,pagenum);
			})
		},
		//渲染页面
		render:function(){
			getlist(curpage,pagenum);
		}
	};
	Page.init();
})();
